<template>
  <div>
    角色分配权限
    <el-tree ref="tree" node-key="id" :data="treeList" :props="{label:'name'}" default-expand-all :show-checkbox="true" :check-strictly="true" />
    <div style="margin-top:20px;text-align:right">
      <el-button type="primary" @click="hAssignPer">确定</el-button>
      <el-button @click="hCancel">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permissions'
import { tranDataToTree } from '@/utils'
import { getRolesDetail, assignPerm } from '@/api/setting'
export default {
  props: {
    id: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      treeList: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async hAssignPer() {
      try {
        const permIds = this.$refs.tree.getCheckedKeys()
        console.log('permIds', permIds)
        await assignPerm({ id: this.id, permIds: permIds })
        this.$emit('close')
      } catch (error) {
        console.log('hAssignPer', error)
      }
    },
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log('getPermissionList', res)
        this.treeList = tranDataToTree(res.data)
        const info = await getRolesDetail(this.id)
        console.log('getRolesDetail', info)
        this.$refs.tree.setCheckedKeys(info.data.permIds)
      } catch (error) {
        console.log('getPermissionList', error)
      }
    },
    hCancel() {
      this.$emit('close')
    }
  }
}
</script>

